package com.dmxh.service.impl;

import com.dmxh.api.ItemAPI;
import com.dmxh.api.UserAPI;
import com.dmxh.mapper.TbOrderItemMapper;
import com.dmxh.mapper.TbOrderMapper;
import com.dmxh.pojo.TbAddress;
import com.dmxh.pojo.TbItem;
import com.dmxh.pojo.TbOrder;
import com.dmxh.pojo.TbOrderItem;
import com.dmxh.service.OrderService;
import com.dmxh.util.IdWorker;
import com.dmxh.vo.AddressVo;
import com.dmxh.vo.CartlistVo;
import com.dmxh.vo.DetailVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private TbOrderMapper tbOrderMapper;
    @Autowired
    private TbOrderItemMapper tbOrderItemMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private UserAPI userAPI;
    @Autowired
    private ItemAPI itemAPI;

    @Override
    @Transactional
    public void saveOrder(Map<String, Object> map) {

        String payType = (String) map.get("payType");
        long addressId = Long.valueOf( map.get("addressId").toString());

        AddressVo addressVo = (AddressVo) userAPI.queryAddressById(addressId).getData();
        TbAddress tbAddress = new TbAddress();
        BeanUtils.copyProperties(addressVo, tbAddress);

        List<CartlistVo> cartList = (List<CartlistVo>) redisTemplate.boundHashOps("cartList").get(tbAddress.getUserId());

        ArrayList<TbOrder> tbOrders = new ArrayList<>();
        ArrayList<TbOrderItem> tbOrderItems = new ArrayList<>();
        //创建item集合
        ArrayList<TbItem> tbItems = new ArrayList<>();
        for (CartlistVo cartlistVo : cartList) {
            TbOrder tbOrder = new TbOrder();
            tbOrder.setOrderId(idWorker.nextId());
            tbOrder.setPaymentType(payType);
            tbOrder.setStatus("1");
            tbOrder.setCreateTime(new Date());
            tbOrder.setUserId(tbAddress.getUserId());
            tbOrder.setReceiverAreaName(tbAddress.getAddress());
            tbOrder.setReceiverMobile(tbAddress.getMobile());
            tbOrder.setReceiver(tbAddress.getContact());
            tbOrder.setSellerId(cartlistVo.getSeller());
            tbOrders.add(tbOrder);
            List<DetailVo> list = cartlistVo.getList();
            for (DetailVo detailVo : list) {
                //创建订单详情对象
                TbOrderItem tbOrderItem = new TbOrderItem();
                //获取扣减库存对象-->设置扣减后的库存
                TbItem tbItem = itemAPI.queryById(detailVo.getItemId());
                tbItem.setStockCount(tbItem.getStockCount()-detailVo.getNum());
                if (tbItem.getStockCount() - detailVo.getNum() < 0) {
                    System.out.println("库存不足");
                    return;
                }
                tbItems.add(tbItem);

                BeanUtils.copyProperties(detailVo,tbOrderItem);
                tbOrderItem.setId(idWorker.nextId());
                tbOrderItem.setOrderId(tbOrder.getOrderId());
                tbOrderItems.add(tbOrderItem);
            }
        }
        //批量更新库存
        itemAPI.defStockCount(tbItems);
        redisTemplate.boundHashOps("cartList").delete(tbAddress.getUserId());

        tbOrderMapper.BatchInsert(tbOrders);
        tbOrderItemMapper.BatchInsert(tbOrderItems);

    }
}
